package cn.wolfcode.car.business.web.controller;


import cn.wolfcode.car.business.domain.CarPackageAudit;
import cn.wolfcode.car.business.domain.ServiceItem;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.mapper.CarPackageAuditMapper;
import cn.wolfcode.car.business.query.CarPackageAuditQuery;
import cn.wolfcode.car.business.service.ICarPackageAuditServicePlus;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.web.AjaxResult;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.activiti.engine.RuntimeService;
import org.apache.poi.util.IOUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.xpath.operations.Mod;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

/**
 * 服务单项控制器
 */
@Controller
@RequestMapping("business/carPackageAudit")
public class CarPackageAuditController {
    //模板前缀
    private static final String prefix = "business/carPackageAudit/";

    @Autowired
    private ICarPackageAuditServicePlus carPackageAuditServicePlus;
    @Autowired
    private RuntimeService runtimeService;


    //页面------------------------------------------------------------
    //列表
    @RequiresPermissions("business:carPackageAudit:view")
    @RequestMapping("/listPage")
    public String listPage(){
        return prefix + "list";
    }

    @RequiresPermissions("business:carPackageAudit:add")
    @RequestMapping("/addPage")
    public String addPage(){
        return prefix + "add";
    }


    @RequestMapping("/todoPage")
    public String todoPage(){
        return prefix + "todoPage";
    }

    @RequestMapping("/donePage")
    public String donePage(){
        return prefix + "donePage";
    }


    @RequiresPermissions("business:carPackageAudit:edit")
    @RequestMapping("/editPage")
    public String editPage(Long id, Model model){
        model.addAttribute("carPackageAudit", carPackageAuditServicePlus.getById(id));
        return prefix + "edit";
    }

    //数据-----------------------------------------------------------
    //列表
    @RequiresPermissions("business:carPackageAudit:list")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<CarPackageAudit> query(CarPackageAuditQuery qo){
        return carPackageAuditServicePlus.query(qo);
    }


    @RequestMapping("/todoQuery")
    @ResponseBody
    public TablePageInfo<CarPackageAudit> todoQuery(CarPackageAuditQuery qo){
        //因为是要查询待办任务
        //需要通过条件进行查询，只查询当前的登录用户所拥有的需要审核的流程，并且是状态是处于进行中的
        qo.setAuditorId(ShiroUtils.getUserId());
        qo.setStatus(CarPackageAudit.STATUS_IN_ROGRESS);
        return carPackageAuditServicePlus.query(qo);
    }

    //新增
    @RequiresPermissions("business:carPackageAudit:add")
    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CarPackageAudit carPackageAudit){
        carPackageAuditServicePlus.save(carPackageAudit);
        return AjaxResult.success();
    }

    //编辑
    @RequiresPermissions("business:carPackageAudit:edit")
    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult edit(CarPackageAudit carPackageAudit){
        QueryWrapper<CarPackageAudit> wrapper = new QueryWrapper<>();
        wrapper.eq("id",carPackageAudit.getId());
        carPackageAuditServicePlus.update(carPackageAudit,wrapper);
        return AjaxResult.success();
    }

    @RequestMapping("/processImg")
    @ResponseBody
    public void processImg(Long id, HttpServletResponse response) throws IOException {
        InputStream in = carPackageAuditServicePlus.getProcessImgAsStream(id);
        IOUtils.copy(in,response.getOutputStream());
    }

    @RequestMapping("/cancelApply")
    @ResponseBody
    public AjaxResult cancelApply(Long id){
        carPackageAuditServicePlus.cancelApply(id);
        return AjaxResult.success();
    }


    @RequestMapping("/auditPage")
    public String auditPage(Long id, Model model){
        model.addAttribute("id",id);
        return prefix + "audit";
    }

    @RequestMapping("/audit")
    @ResponseBody
    public AjaxResult audit(Long id,Integer auditStatus,String info){
        carPackageAuditServicePlus.audit(id,auditStatus,info);
        return AjaxResult.success();
    }


    @RequestMapping("/doneQuery")
    @ResponseBody
    public TablePageInfo<CarPackageAudit> doneQuery(CarPackageAuditQuery qo){
        //因为info信息中都含有审批人的username，所有在查询自己的已办审核记录时只需模糊匹配查询即可
        qo.setInfo(ShiroUtils.getUser().getUserName());
        return carPackageAuditServicePlus.query(qo);
    }




}
